App.controller('busStickerWorkbillController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {
    Page.ajaxPost('employeeGroup/empTree.do', null, function(resp) {
        $scope.empList = resp.data;
    });
	
	$scope.orderType = '1';
	$scope.workbillType = '1';
	$scope.workflowCode = 'workbill';
	
	Page.ajaxPostQuiet('customer/list.do', null, function (resp) {
        $scope.customerList = resp.data;
	});
	
	Page.ajaxPostQuiet('employeeGroup/groupList.do', null, function (resp) {
        $scope.groupList = resp.data;
	});
	
	$scope.getTaskSteps = function(){
		var steps = [];
    	$($rootScope.options.task_type).each(function(i,s){
    		if(s.value == '1' || s.value == '2' || s.value == '3'){
    			steps.push({
        			id : s.value,
        			name : s.name
        		});
    		}
    	});
    	return steps;
	};
	
	$scope.getCustomerName = function(customerId){
		var customerName = '';
  	  	$($scope.customerList).each(function(idx,customer){
  	  		if(customer.customerId == customerId){
  	  			customerName = customer.customerName;
  	  			return false;
  	  		}
  	  	});
  	  	return customerName;
	};
	
    $scope.grid = {
        column_defs: [
          {
        	  target : 1,
              template: function(row) {
            	  return '<a href="javascript:void(0);" ed-click="viewWorkbill">' + row.workbillId + '</a>';
              }
          },{
        	  target : 3,
              template: function(row) {
                  return $scope.getCustomerName(row.customerId);
              }
          },{
        	  target : 5,
              template: function(row) {
            	  var html = '';
            	  $(row.workbillItems).each(function(i,workbillItem){
            		  html += workbillItem.orderItem.busVehicleNo + '  ';
            		  html += (i+1)%5 == 0 ? '\n' : '';
            	  });
            	  return html;
              }
          },{
            target : 7,
            buttons : [
                {
                    name: '下发',
                    class: 'btn-warning btm-sm',
                    icon: 'fa-edit',
                    visiable: function(row) {
                        return row.status && row.status == '0';
                    },
                    event: function(row){
                    	$scope.workbill = row;
                    	$scope.steps = $scope.getTaskSteps();
                    	$scope.workbillSendModal.show();
                    }
                },
                {
                    name: '跟踪',
                    class: 'btn-warning btm-sm',
                    icon: 'fa-edit',
                    visiable: function(row) {
                        return row.status && row.status == '1';
                    },
                    event: function(row){
                    	$scope.workbill = row;
                    	$scope.steps = $scope.getTaskSteps();
                    	Page.ajaxPostQuiet('workbill/task/tasksOfWorkbill.do', {workbillId : $scope.workbill.workbillId}, function (resp) {
                            $scope.workbill.tasks = resp.data;
                            $scope.workbillTasksModal.show();
                    	});
                    }
                }
            ]
        }],
        viewWorkbill : function(row){
        	$scope.workbill = row;
        	$scope.workbillModal.show();
        },
        sendWorkBill : function(){
        	var f = true;
        	$($scope.steps).each(function(idx,step){
      		  	if(step.empId == ''){
      		  		Page.alert('请选择责任人');
      		  		f = false;
      		  		return false;
      		  	}
      	  	});
        	if(f){
        		Page.ajaxPostQuiet('workbill/send.do', {workbillId : $scope.workbill.workbillId, sendSteps : $scope.steps}, function (resp) {
            		$scope.workbillSendModal.hide();
            		$scope.grid.query();
            	});
        	}
        }
    };
    
    


}]);